package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

    /**
     * 初始化账户
     * @param userId
     */
    @Override
    public void initUserAccount(Long userId) {
        //查询用户是否存在，防止重复消费
        long count = count(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));
        if (count >0) {
            return;
        }
        //初始化
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        if (!save(userAccount)) {
            throw new GuiguException(201, "初始化失败");
        }

    }
}
